// 金山词霸
/*
  地址： https://ifanyi.iciba.com/index.php?auth_user=key_ciba&c=trans&m=fy&client=6&sign=0020c1fc11e96d3a
    经过分析 每次搜索地址中的 sign 都会变化 
      sign是通过 字符串：6key_cibaifanyicjbysdlove1 + 翻译的文字 整体通过md5加密算法之后截取前16位字符即可
  请求方式：POST
  请求体：
    form：zh
    to:en
    q:要翻译的内容

  返回的结果： content: {from: "zh", to: "en", vendor: "ciba", out: "Hello", reqid: "c0e18843-db5b-4222-9353-6c0891bb2505",…}
              ciba_out: ""
              ciba_use: "以上结果来自机器翻译。"
              err_no: 0
              from: "zh"
              out: "Hello"  // 这个就是翻译的结果
              reqid: "c0e18843-db5b-4222-9353-6c0891bb2505"
              to: "en"
              ttsLan: 1
              vendor: "ciba"
              version: "v2.15.210422.1"
              status: 1
*/
const md5 = require('md5')
const axios = require('axios')
const qs = require('qs')

class Translation {
	constructor() {}

	async getTranslation(str) {
		return await axios.post(
			`https://ifanyi.iciba.com/index.php?auth_user=key_ciba&c=trans&m=fy&client=6&sign=${this.getSign(
				str
			)}`,
			qs.stringify({ from: 'zh', to: 'en', q: str })
		)
	}

	getSign(str) {
		return md5(`6key_cibaifanyicjbysdlove1${str}`).slice(0, 16)
	}
}

const MyTranslation = new Translation()

MyTranslation.getTranslation('在干嘛').then(res => {
	console.log(res)
})

// console.log(res)
